﻿using PTop.Code;
using PTop.Domain.Entity.WeChat;
using PTop.Domain.IRepository.WeChat;
using PTop.Repository.WeChat;
using System.Collections.Generic;

namespace PTop.Application.WeChat
{
    /// <summary>
    /// 微信用户信息App
    /// </summary>	
    public class WxUserInfoApp
    {
        private IWxUserInfoRepository service = new WxUserInfoRepository();

        public List<WxUserInfoEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<WxUserInfoEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_OpenId.Contains(keyword));
                expression = expression.Or(t => t.F_NickName.Contains(keyword));
            }
            return service.FindList(expression, pagination);
        }
        public WxUserInfoEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void Delete(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }

        public void SubmitForm(WxUserInfoEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                service.Update(entity);
            }
            else
            {
                var old = GetFormByOpenId(entity.F_OpenId);
                if (old != null)
                {
                    service.Delete(old);
                }
                service.Insert(entity);
            }
        }

        #region For Front

        public WxUserInfoEntity GetFormByOpenId(string openId)
        {
            var uie = service.FindEntity(m => m.F_OpenId == openId);
            return uie;
        }

        #endregion

    }
}